﻿package mortal.game.view.lotteryDraw
{
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.view.ThanksGiving.command.*;
    import mortal.game.view.common.*;
    import mortal.game.view.wizard.*;

    public class lotteryDrawModule extends BaseWindow
    {
        private var _tabData:Array;
        private var _tabBar:GTabarNew;
        private var _curSelIndex:int = 0;
        private var _tabPanelObj:Object;
        private var _recodeView:lotteryDrawRecordView;
        private var _curPanel:GSprite;
        private var _activeTipView:LotteryDrawActiveTipView;
        private static var isShowAnjinPanel:Boolean = true;
        public static var openBoxLevel:int = -1;

        public function lotteryDrawModule()
        {
            this.setSize(930, 603);
            this.title = Language.getString(90426);
            return;
        }// end function

        public function resetTabData() : void
        {
            this._tabData = [];
            var _loc_1:* = Cache.instance.role.entityInfo.level;
            var _loc_2:int = 0;
            while (_loc_2 < lotteryDrawConst.tabOpenLevelArray.length)
            {
                
                if (_loc_1 >= lotteryDrawConst.tabOpenLevelArray[_loc_2])
                {
                    this._tabData.push(lotteryDrawConst.tabDataArray[_loc_2]);
                }
                _loc_2++;
            }
            this._tabData.push(lotteryDrawConst.tabDataArray[(lotteryDrawConst.tabDataArray.length - 1)]);
            if (this._tabBar)
            {
                this._tabBar.dataProvider = this._tabData;
            }
            return;
        }// end function

        public function updateActivePropNum() : void
        {
            if (this._activeTipView != null)
            {
                this._activeTipView.updateData();
            }
            return;
        }// end function

        private function onSwitchTab(event:MuiEvent = null) : void
        {
            if (this._tabBar.selectedIndex == (this._tabData.length - 1))
            {
                this.switchPanel(1);
            }
            else
            {
                this.switchPanel(0);
            }
            return;
        }// end function

        private function addExchangeView() : void
        {
            this._curPanel = UICompomentPool.getUICompoment(LotteryExchangePanel);
            this._curPanel.x = 0;
            this._curPanel.y = 0;
            this.addChild(this._curPanel);
            return;
        }// end function

        private function addPlayView() : void
        {
            this._curPanel = UICompomentPool.getUICompoment(lotteryDrawPlayView);
            this._curPanel.x = 0;
            this._curPanel.y = 0;
            this.addChild(this._curPanel);
            return;
        }// end function

        private function addRecodeView() : void
        {
            this._recodeView = UICompomentPool.getUICompoment(lotteryDrawRecordView);
            this._recodeView.x = 645;
            this._recodeView.y = 35;
            this.addChild(this._recodeView);
            return;
        }// end function

        private function removeRecodeView() : void
        {
            if (this._recodeView)
            {
                this._recodeView.dispose();
                this._recodeView = null;
            }
            return;
        }// end function

        private function switchPanel(param1:int) : void
        {
            if (this._curPanel)
            {
                this._curPanel.dispose();
                this._curPanel = null;
            }
            this.removeRecodeView();
            switch(param1)
            {
                case 0:
                {
                    this.addPlayView();
                    this.addRecodeView();
                    this.panelData = lotteryDrawConst.tabPanelDataArray[this._tabBar.selectedIndex];
                    this.showActiveTipView(true);
                    break;
                }
                case 1:
                {
                    this.addExchangeView();
                    this.showActiveTipView(false);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function set panelData(param1:Object) : void
        {
            if (this._curPanel is lotteryDrawPlayView)
            {
                (this._curPanel as lotteryDrawPlayView).panelData = param1;
            }
            if (this._recodeView && !this._recodeView.isDisposed)
            {
                this._recodeView.panelData = param1;
            }
            return;
        }// end function

        public function updateMyExchangeRecord(param1:int) : void
        {
            if (this._curPanel is LotteryExchangePanel)
            {
                (this._curPanel as LotteryExchangePanel).updateMyRecord(param1);
            }
            return;
        }// end function

        public function updateBAreaExchangeRecord(param1:int) : void
        {
            if (this._curPanel is LotteryExchangePanel)
            {
                (this._curPanel as LotteryExchangePanel).updateBAreaRecord(param1);
            }
            return;
        }// end function

        public function updateMyRecord(param1:int) : void
        {
            if (this._recodeView && !this._recodeView.isDisposed)
            {
                this._recodeView.updateMyRecord(param1);
            }
            return;
        }// end function

        public function updateBAreaRecord(param1:int) : void
        {
            if (this._recodeView && !this._recodeView.isDisposed)
            {
                this._recodeView.updateBAreaRecord(param1);
            }
            return;
        }// end function

        public function updateRareRecord(param1:int) : void
        {
            if (this._recodeView && !this._recodeView.isDisposed)
            {
                this._recodeView.updateRareRecord(param1);
            }
            return;
        }// end function

        public function setDefault() : void
        {
            this.switchPanel(0);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.resetTabData();
            this._tabBar = UIFactory.gTabBarNew(0, 32, this._tabData, 930, 600, 65 + 40, 26, this, this.onSwitchTab, "TabButtonNew");
            this.setDefault();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._tabBar.dispose(param1);
            this._tabBar = null;
            openBoxLevel = -1;
            if (this._curPanel)
            {
                this._curPanel.dispose(param1);
                this._curPanel = null;
            }
            if (this._recodeView)
            {
                this._recodeView.dispose(param1);
                this._recodeView = null;
            }
            super.disposeImpl(param1);
            this.showActiveTipView(false);
            return;
        }// end function

        public function refresh() : void
        {
            this.panelData = lotteryDrawConst.tabPanelDataArray[this._tabBar.selectedIndex];
            return;
        }// end function

        override protected function onWindowDownHandler(event:MouseEvent) : void
        {
            super.onWindowDownHandler(event);
            if (lotteryDrawAnjinSkillWin.isViewShow())
            {
                lotteryDrawAnjinSkillWin.instance.layer.setTop(lotteryDrawAnjinSkillWin.instance);
            }
            return;
        }// end function

        override protected function onMouseMoveHandler(event:MouseEvent) : void
        {
            super.onMouseMoveHandler(event);
            if (lotteryDrawAnjinSkillWin.isViewShow())
            {
                lotteryDrawAnjinSkillWin.instance.x = this.x + this.width + 5;
                lotteryDrawAnjinSkillWin.instance.y = this.y;
            }
            return;
        }// end function

        override public function stageResize() : void
        {
            super.stageResize();
            if (lotteryDrawAnjinSkillWin.isViewShow())
            {
                LayerManager.windowLayer.centerWindows(this, lotteryDrawAnjinSkillWin.instance);
            }
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            if (lotteryDrawAnjinSkillWin.isViewShow())
            {
                lotteryDrawAnjinSkillWin.instance.hide();
            }
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            var _loc_4:int = 0;
            super.show(param1, param2);
            this.x = Global.stage.stageWidth - this.width >> 1;
            this.y = Global.stage.stageHeight - this.height >> 1;
            var _loc_3:* = Cache.instance.role.entityInfo.level;
            if (isShowAnjinPanel)
            {
                if (_loc_3 < lotteryDrawConst.tabOpenLevelArray[1])
                {
                    lotteryDrawAnjinSkillWin.instance.show();
                    LayerManager.windowLayer.centerWindows(this, lotteryDrawAnjinSkillWin.instance);
                    isShowAnjinPanel = false;
                }
            }
            if (openBoxLevel > 0)
            {
                _loc_4 = 0;
                while (_loc_4 < lotteryDrawConst.tabOpenLevelArray.length)
                {
                    
                    if (openBoxLevel == lotteryDrawConst.tabOpenLevelArray[_loc_4])
                    {
                        this._tabBar.selectedIndex = _loc_4;
                    }
                    _loc_4++;
                }
            }
            else
            {
                _loc_4 = 0;
                while (_loc_4 < lotteryDrawConst.tabOpenLevelArray.length)
                {
                    
                    if (_loc_3 >= lotteryDrawConst.tabOpenLevelArray[_loc_4])
                    {
                        this._tabBar.selectedIndex = _loc_4;
                    }
                    _loc_4++;
                }
            }
            this.switchPanel(0);
            return;
        }// end function

        private function showActiveTipView(param1:Boolean) : void
        {
            if (ThanksGivingUtil2.isInActive && param1)
            {
                if (this._activeTipView != null)
                {
                    this._activeTipView.dispose();
                    this._activeTipView = null;
                }
                this._activeTipView = new LotteryDrawActiveTipView();
                this._activeTipView.updateData();
                this._activeTipView.move(19, 170);
                this.addChild(this._activeTipView);
            }
            else if (this._activeTipView != null)
            {
                this._activeTipView.dispose();
                this._activeTipView = null;
            }
            return;
        }// end function

        public function lotteryOpenExchengePanel() : void
        {
            this._tabBar.selectedIndex = this._tabData.length - 1;
            this.onSwitchTab();
            return;
        }// end function

    }
}
